#include "Application.h"

IMPLEMENT_APP(Application);

bool Application::OnInit()
{
	_renderLoopOn = false;

	wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
	MainWindow *frame = new MainWindow(wxT("Application wxWidget"));

	drawPanel = new DrawingPanel(frame);
	sizer->Add((wxWindow*)drawPanel, 1, wxEXPAND);

	frame->SetSizer(sizer);
	frame->Show(true);

	ActivateRenderLoop(true);

	return true;
}

void Application::OnIdle(wxIdleEvent &evt)
{
	if (_renderLoopOn)
	{
		drawPanel->Idle();
        evt.RequestMore(); // render continuously, not only once on idle
		Sleep(10);
	}
}

void Application::ActivateRenderLoop(bool on)
{
	if(on && !_renderLoopOn)
    {
        Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(Application::OnIdle) );
        _renderLoopOn = true;
    }
    else if(!on && _renderLoopOn)
    {
        Disconnect(wxEVT_IDLE, wxIdleEventHandler(Application::OnIdle) );
        _renderLoopOn = false;
    }
}